﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using GenesisDemo.Enums;

namespace GenesisDemo.Helper
{
    public class Genesis
    {
        //gateway的运行命令色这个样子的 gateway <address> <message>
        //address为genesis上的数据： 用户名@机器名.机器名
        //这里我只取第一个用户名为脚本执行的地方
        /// <summary>
        /// 程序运行的对应用户地址
        /// </summary>
        public List<string> Address { get; set; }

        /// <summary>
        ///GENESIS的根目录
        /// </summary>
        string GenesisRootDIR { get; set; }

        /// <summary>
        ///GENESIS的程序目录
        /// </summary>
        string GenesisDIR { get; set; }

        /// <summary>
        /// 发送指令给genesis
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        public Genesis(string genesisRootDIR, string genesisDIR)
        {
            GenesisRootDIR = genesisRootDIR;
            GenesisDIR = genesisDIR;
            Address = new List<string>();
            //返回结果以的用户以\r\n隔开要替换掉为,再分组
            string s = SendCommand($"\"{Enum.GetName(typeof(GenesisCommandEnum), GenesisCommandEnum.WHO)}\"");
            string[] users = s.Trim().Replace(' ', ',').Split(',');
            foreach (var item in users)
            {
                Address.Add(item);
            }
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        public string SendCommand(string cmdText)
        {
            Process p = new Process();
            p.StartInfo.FileName = GenesisDIR + @"\misc\gateway.exe";
            //返回错误提示
            p.StartInfo.RedirectStandardError = true;
            //可以发送指令
            p.StartInfo.RedirectStandardInput = true;
            //可以用接受指令执行结果
            p.StartInfo.RedirectStandardOutput = true;
            //执行命令不显示窗口
            p.StartInfo.CreateNoWindow = true;
            ///执行指令
            p.StartInfo.Arguments = cmdText;
            p.StartInfo.UseShellExecute = false;
            //
            p.Start();
            //接收指令执行结果
            StreamReader read = p.StandardOutput;
            string s = read.ReadToEnd();
            //结束进程
            p.Close();
            return string.IsNullOrEmpty(s) ? "" : s;
        }

        /// <summary>
        /// 向指定的ip发送数据
        /// </summary>
        /// <param name="address">ip地址</param>
        /// <param name="command">命令</param>
        /// <param name="cmdText">内容</param>
        /// <returns></returns>
        public string Send(string address, GenesisCommandEnum command, string cmdText)
        {
            return SendCommand(address + $" \"{Enum.GetName(typeof(GenesisCommandEnum), command)} {cmdText}\"");
        }

        /// <summary>
        /// 向指定的ip发送命令
        /// </summary>
        /// <param name="addressIndex">地址在Address中的索引</param>
        /// <param name="command">命令</param>
        /// <param name="cmdText">内容</param>
        /// <returns></returns>
        public string Send(int addressIndex, GenesisCommandEnum command, string cmdText)
        {
            return SendCommand(Address[addressIndex] + $" \"{Enum.GetName(typeof(GenesisCommandEnum), command)} {cmdText}\"");
        }

        /// <summary>
        /// 向检测到的局域网下所有的客户端进行发送数据
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="cmdText">内容</param>
        /// <returns></returns>
        public Dictionary<string, string> Send2All(GenesisCommandEnum command, string cmdText)
        {

            Dictionary<string, string> returnList = new Dictionary<string, string>();
            foreach (var item in Address)
            {
                returnList.Add(item, SendCommand(item + $" \"{Enum.GetName(typeof(GenesisCommandEnum), command)} {cmdText}\""));
            }
            return returnList;
        }
    }

}